package hotel.callback.oversea.service;

import com.alibaba.fastjson.JSON;
import hotel.callback.oversea.mapper.SwitchsNotifyShortLogMapper;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.enums.MtGoodsEnums;
import hotel.base.oversea.utils.RestTemplateUtils;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.utils.StringUtil;
import hotel.callback.oversea.entity.switchs.SwitchsNotifyShortLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.Map;

/**
 * 相关restTemplate 接口调用工具实现类
 *
 * @Author lmf
 * @Date 2020/8/5 10:29
 **/
@Slf4j
@Service
public class SwitchRestTempaleUtilsService {

    /**
     * @author: lmf
     * @date: 2020/9/3 11:05
     * 推送记录请求到美团日志文件接口
     */
    @Autowired
    SwitchsNotifyShortLogMapper switchsNotifyShortLogMapper;

    @Autowired
    RestTemplate restTemplate;

    public void pushSavePopLog(int hotelId, int mtHotelId, String timestampFromMtData) {

        SwitchsNotifyShortLog switchsNotifyShortLog = new SwitchsNotifyShortLog();
        switchsNotifyShortLog.setId(StringUtil.getUUID());
        switchsNotifyShortLog.setTimestamp(timestampFromMtData);
        switchsNotifyShortLog.setCreateTime(new Date());
        switchsNotifyShortLog.setMtHotelId(String.valueOf(mtHotelId));
        switchsNotifyShortLog.setSwHotelId(String.valueOf(hotelId));
        switchsNotifyShortLog.setOprate(MtGoodsEnums.goodsChangeType.POP.getCode());
        // 10 异步价格  11 主动查询价格 20 异步房态  21 主动查询房态 30 携程推送成功数据
        switchsNotifyShortLog.setSource(MtGoodsEnums.goodsChangeSource.SWITCHS_PUSH.getCode());
        try {
            switchsNotifyShortLogMapper.insertEntity(switchsNotifyShortLog);
        } catch (Exception e) {
            log.info("异步通知有变化数据保存日志switchsNotifyShortLog异常{},{}",e.getMessage(), JSON.toJSONString(switchsNotifyShortLog));
            e.printStackTrace();
        }

        /*Map map= new HashMap();
        try{
            map.put("hotelId",String.valueOf(hotelId));
            map.put("mtHotelId",String.valueOf(mtHotelId));
            map.put("timestamp",timestampFromMtData);
            //发起请求
            Result response = RestTemplateUtils.post(CommonUrlConstants.MEITUAN_HOST+"/mtNotifyShortLog/popSaveLog", JSON.toJSONString(map),restTemplate);
            if(CommonConstants.SUCCESS==response.getCode()){
            }else{
                log.error("////////////////////////pushRoom 保存队列日志异常：{},{}",JSON.toJSONString(map),JSON.toJSONString(response));
            }
        }catch (Exception e){
            log.error("pushRoom {} >>>>>>>>> 保存队列日志   异常catch：{},{},{}", DateUtil.getNowTime(),JSON.toJSONString(map),e.getMessage());
            e.printStackTrace();
        }*/


    }
    /***
    * @Author: lmf
    * @Date: 2021/3/12
     * 异步推送数据到表里
    */
    public void pushHotelLog(Map<String, Object> params) {
        Result response = RestTemplateUtils.post(CommonUrlConstants.SWITCHS_ONLINE_HOST+"/switchHotelUp/addPushHotelLog", JSON.toJSONString(params),restTemplate);
        if(CommonConstants.SUCCESS==response.getCode()){
        }else{
            log.error("////////////////////////pushHotelLog 保存队列日志异常：{},{}", JSON.toJSONString(params), JSON.toJSONString(response));
        }
    }
}
